package com.example.zzj.ziwuyundemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.zzj.ziwuyundemo.R;
import com.example.zzj.ziwuyundemo.entity.DetailEntity;
import com.example.zzj.ziwuyundemo.entity.DriverEntity;
import com.example.zzj.ziwuyundemo.entity.SupercargoEntity;
import com.example.zzj.ziwuyundemo.util.Cn2Spell;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by zzj on 2016/12/19.
 */

public class DiverRecyclerAdapter extends RecyclerView.Adapter<DiverRecyclerAdapter.MyHolder> {
    private Context context;
    private  List<DriverEntity.DriverpageBean> driverpageBeen;
    private Cn2Spell cn2Spell = new Cn2Spell();

    public OnItemClickListener mOnItemClickListener;//接口实例

    public DiverRecyclerAdapter(Context mContext, List<DriverEntity.DriverpageBean> driverpageBeen) {
        this.context =mContext;
        this.driverpageBeen = driverpageBeen;
    }

//    public void  addDiver(List<DriverEntity.DriverpageBean> driverpageBeenList){
//        driverpageBeen = driverpageBeenList;
//        Log.d("driverpage",driverpageBeen.toString());
//    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dialog_preson_item, parent, false);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        /**排序*/
        Collections.sort(driverpageBeen, new Comparator<DriverEntity.DriverpageBean>() {
            @Override
            public int compare(DriverEntity.DriverpageBean o1, DriverEntity.DriverpageBean o2) {
                String pinyinName1 = cn2Spell.getPinYin(o1.getName());
                String pinyinName2 = cn2Spell.getPinYin(o2.getName());
                char c1 = pinyinName1.toString().charAt(0);
                char c2 = pinyinName2.toString().charAt(0);
                if (c1 >= 'a' && c1 <= 'z') {
                    c1 -= 32;
                }
                if (c2 >= 'a' && c2 <= 'z') {
                    c2 -= 32;
                }
                int result = c1 > c2 ? 1 : (c1 == c2 ? 0 : -1);
                if (result == 0) {
                    /**再进行一次比较*/
                    result = o1.toString().compareTo(o2.toString());
                }
                return result;
            }
        });
        /**获取名字拼音的首字母*/
        char c1 = cn2Spell.getPinYin(driverpageBeen.get(position).getName()).charAt(0);
        if (c1 >= 'a' && c1 <= 'z') {
            c1 -= 32;
        }
        /**在布局上加数据*/
        if (position == 0) {
            holder.tag.setText(String.valueOf(c1));
            holder.tag.setVisibility(View.VISIBLE);
        } else {
            DriverEntity.DriverpageBean driverpageBean = driverpageBeen.get(position - 1);
            char c2 = cn2Spell.getPinYin(driverpageBean.getName()).charAt(0);
            if (c2 >= 'a' && c2 <= 'z') {
                c2 -= 32;
            }
            if (c1 == c2) {
                holder.tag.setVisibility(View.GONE);
            } else {
                holder.tag.setVisibility(View.VISIBLE);
                holder.tag.setText(String.valueOf(c1));
            }

        }
        holder.line.setVisibility(View.VISIBLE);
        holder.name.setText(driverpageBeen.get(position).getName());
        holder.phone.setText(driverpageBeen.get(position).getPhoneNumbChief());

        if (mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
    }
    @Override
    public int getItemCount() {
        return driverpageBeen.size();
    }

    //接口回调设置点击事件
    public interface OnItemClickListener{
        void onItemClick(View view ,int position);

    }

    //设置点击事件
    public void setOnItemClickListener(OnItemClickListener listener){
        this.mOnItemClickListener = listener;
    }
    class MyHolder extends RecyclerView.ViewHolder {
        private TextView name,phone,tag;
        private View line;
        public MyHolder(View itemView) {
            super(itemView);
            tag = (TextView) itemView.findViewById(R.id.tag);
            name = (TextView) itemView.findViewById(R.id.name);
            phone = (TextView) itemView.findViewById(R.id.phone);
            line = itemView.findViewById(R.id.line);
        }
    }
}
